package zdream.pmfield.storage;

import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonParser;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * @author Zdream
 * @date 2023-11-10
 * @since 0.0.1
 */
public class JsonLoader {
	public static JsonElement fromPath(Path path) {
		String json;
		try {
			byte[] bytes = Files.readAllBytes(path);
			json = new String(bytes, StandardCharsets.UTF_8);
		} catch (IOException e) {
			e.printStackTrace();
			return JsonNull.INSTANCE;
		}

		return JsonParser.parseString(json);
	}

	public static JsonElement fromInputStream(InputStream stream) {
		return JsonParser.parseReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
	}
}
